package org.lanyu.springainovel.common.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lanyu.springainovel.common.service.FileSystemMcpService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 文件系统MCP服务控制�? * 提供REST API接口来调用文件系统MCP服务
 */
@Slf4j
@RestController
@RequestMapping("/filesystem")
@RequiredArgsConstructor
@Tag(name = "文件系统MCP服务", description = "提供文件系统操作的API接口")
public class FileSystemMcpController {
    
    private final FileSystemMcpService fileSystemMcpService;
    
    /**
     * 读取文件内容
     */
    @GetMapping("/read")
    @Operation(summary = "读取文件内容", description = "读取指定路径的文件内")
    public ResponseEntity<String> readFile(
            @Parameter(description = "文件路径") @RequestParam String filePath,
            @Parameter(description = "允许的目录") @RequestParam(required = false) String allowedDirectory) {
        
        try {
            String content = fileSystemMcpService.readFile(filePath, allowedDirectory);
            return ResponseEntity.ok(content);
        } catch (Exception e) {
            log.error("读取文件失败", e);
            return ResponseEntity.badRequest().body("读取文件失败: " + e.getMessage());
        }
    }
    
    /**
     * 写入文件内容
     */
    @PostMapping("/write")
    @Operation(summary = "写入文件内容", description = "向指定路径写入文件内")
    public ResponseEntity<String> writeFile(
            @Parameter(description = "文件路径") @RequestParam String filePath,
            @Parameter(description = "文件内容") @RequestBody String content,
            @Parameter(description = "允许的目录，") @RequestParam(required = false) String allowedDirectory) {
        
        try {
            boolean success = fileSystemMcpService.writeFile(filePath, content, allowedDirectory);
            if (success) {
                return ResponseEntity.ok("文件写入成功");
            } else {
                return ResponseEntity.badRequest().body("文件写入失败");
            }
        } catch (Exception e) {
            log.error("写入文件失败", e);
            return ResponseEntity.badRequest().body("写入文件失败: " + e.getMessage());
        }
    }
    
    /**
     * 列出目录内容
     */
    @GetMapping("/list")
    @Operation(summary = "列出目录内容", description = "列出指定目录下的文件和子目录")
    public ResponseEntity<List<String>> listDirectory(
            @Parameter(description = "目录路径") @RequestParam String dirPath,
            @Parameter(description = "允许的目录，可") @RequestParam(required = false) String allowedDirectory) {
        
        try {
            List<String> contents = fileSystemMcpService.listDirectory(dirPath, allowedDirectory);
            return ResponseEntity.ok(contents);
        } catch (Exception e) {
            log.error("列出目录失败", e);
            return ResponseEntity.badRequest().body(List.of("列出目录失败: " + e.getMessage()));
        }
    }
    
    /**
     * 创建目录
     */
    @PostMapping("/mkdir")
    @Operation(summary = "创建目录", description = "创建指定路径的目")
    public ResponseEntity<String> createDirectory(
            @Parameter(description = "目录路径") @RequestParam String dirPath,
            @Parameter(description = "允许的目录，可") @RequestParam(required = false) String allowedDirectory) {
        
        try {
            boolean success = fileSystemMcpService.createDirectory(dirPath, allowedDirectory);
            if (success) {
                return ResponseEntity.ok("目录创建成功");
            } else {
                return ResponseEntity.badRequest().body("目录创建失败");
            }
        } catch (Exception e) {
            log.error("创建目录失败", e);
            return ResponseEntity.badRequest().body("创建目录失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取文件信息
     */
    @GetMapping("/info")
    @Operation(summary = "获取文件信息", description = "获取指定文件或目录的详细信息")
    public ResponseEntity<Map<String, Object>> getFileInfo(
            @Parameter(description = "文件路径") @RequestParam String filePath,
            @Parameter(description = "允许的目录，可") @RequestParam(required = false) String allowedDirectory) {
        
        try {
            Map<String, Object> fileInfo = fileSystemMcpService.getFileInfo(filePath, allowedDirectory);
            return ResponseEntity.ok(fileInfo);
        } catch (Exception e) {
            log.error("获取文件信息失败", e);
            return ResponseEntity.badRequest().body(Map.of("error", "获取文件信息失败: " + e.getMessage()));
        }
    }
    
    /**
     * 移动/重命名文件或目录
     */
    @PostMapping("/move")
    @Operation(summary = "移动/重命名文件或目录", description = "将文件或目录从源路径移动到目标路")
    public ResponseEntity<String> moveFile(
            @Parameter(description = "源路") @RequestParam String sourcePath,
            @Parameter(description = "目标路径") @RequestParam String destinationPath,
            @Parameter(description = "允许的目录，可") @RequestParam(required = false) String allowedDirectory) {
        
        try {
            boolean success = fileSystemMcpService.moveFile(sourcePath, destinationPath, allowedDirectory);
            if (success) {
                return ResponseEntity.ok("文件移动成功");
            } else {
                return ResponseEntity.badRequest().body("文件移动失败");
            }
        } catch (Exception e) {
            log.error("移动文件失败", e);
            return ResponseEntity.badRequest().body("移动文件失败: " + e.getMessage());
        }
    }
    
    /**
     * 搜索文件
     */
    @GetMapping("/search")
    @Operation(summary = "搜索文件", description = "在指定目录中搜索匹配模式的文")
    public ResponseEntity<List<String>> searchFiles(
            @Parameter(description = "目录路径") @RequestParam String dirPath,
            @Parameter(description = "搜索模式") @RequestParam String pattern,
            @Parameter(description = "允许的目录，可") @RequestParam(required = false) String allowedDirectory) {
        
        try {
            List<String> results = fileSystemMcpService.searchFiles(dirPath, pattern, allowedDirectory);
            return ResponseEntity.ok(results);
        } catch (Exception e) {
            log.error("搜索文件失败", e);
            return ResponseEntity.badRequest().body(List.of("搜索文件失败: " + e.getMessage()));
        }
    }

    /**
     * 设置默认允许的目
     */
    @PostMapping("/set-default-directory")
    @Operation(summary = "设置默认允许的目", description = "设置文件系统操作的默认允许目")
    public ResponseEntity<String> setDefaultAllowedDirectory(
            @Parameter(description = "目录路径") @RequestParam String directory) {
        
        try {
            fileSystemMcpService.setDefaultAllowedDirectory(directory);
            return ResponseEntity.ok("默认允许目录设置成功: " + directory);
        } catch (Exception e) {
            log.error("设置默认允许目录失败", e);
            return ResponseEntity.badRequest().body("设置默认允许目录失败: " + e.getMessage());
        }
    }
    
    /**
     * 关闭指定目录的客户端
     */
    @PostMapping("/close-client")
    @Operation(summary = "关闭指定目录的客户端", description = "关闭指定目录的MCP客户端连")
    public ResponseEntity<String> closeClient(
            @Parameter(description = "允许的目") @RequestParam String allowedDirectory) {
        
        try {
            fileSystemMcpService.closeClient(allowedDirectory);
            return ResponseEntity.ok("客户端关闭成");
        } catch (Exception e) {
            log.error("关闭客户端失", e);
            return ResponseEntity.badRequest().body("关闭客户端失�? " + e.getMessage());
        }
    }
    
    /**
     * 关闭所有客户端
     */
    @PostMapping("/close-all-clients")
    @Operation(summary = "关闭所有客户端", description = "关闭所有MCP客户端连")
    public ResponseEntity<String> closeAllClients() {
        try {
            fileSystemMcpService.closeAllClients();
            return ResponseEntity.ok("所有客户端关闭成功");
        } catch (Exception e) {
            log.error("关闭所有客户端失败", e);
            return ResponseEntity.badRequest().body("关闭所有客户端失败: " + e.getMessage());
        }
    }
}
